﻿using System;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.Diagnostics;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using Autodesk.Revit.UI;

namespace YJKMenuAssist.Controls
{
	public partial class LeftTabControl : UserControl
	{
		public event RoutedEventHandler ClickItem;

		private int TITLE_COUNT
		{
			get
			{
				return this.m_CommandTableInfos.TableInofList.Count;
			}
		}

		public CommandTableInfos CmndTableInfos
		{
			set
			{
				this.m_CommandTableInfos = value;
			}
		}

		public LeftTabControl()
		{
			this.InitializeComponent();
		}

		public bool Initialize(MainPage mainPage, ExternalCommandData commandData)
		{
			this.CreateGeneralType();
			this.m_MainPage = mainPage;
			this.m_CommandData = commandData;
			if (this.m_CommandTableInfos == null)
			{
				return false;
			}
			if (this.TITLE_COUNT <= 0)
			{
				return false;
			}
			for (int i = 0; i < this.TITLE_COUNT; i++)
			{
				TableInfos tableInfos = this.m_CommandTableInfos.TableInofList[i];
				this.CreateButton(tableInfos.TableName, i);
			}
			return true;
		}

		private void CreateGeneralType()
		{
			Label label = new Label();
			label.Content = "常用类型";
			label.HorizontalAlignment = HorizontalAlignment.Center;
			this.leftWrapPanel.Children.Add(label);
			this.CreateButton("墙", -1);
			this.CreateButton("门", -1);
			this.CreateButton("窗", -1);
			Label label2 = new Label();
			label2.Content = "常用工具";
			label2.HorizontalAlignment = HorizontalAlignment.Center;
			this.leftWrapPanel.Children.Add(label2);
		}

		private void CreateButton(string buttonName, int index)
		{
			LeftButton leftButton = new LeftButton();
			leftButton.button.Content = buttonName;
			leftButton.button.ToolTip = buttonName;
			leftButton.button.Tag = index;
			leftButton.button.Click += this.btn_ClickItem;
			if (index == 0)
			{
				SolidColorBrush background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#BBCCCCCB"));
				leftButton.button.Background = background;
			}
			this.leftWrapPanel.Children.Add(leftButton);
		}

		private void SetLeftButtonBackGroundToWrite()
		{
			try
			{
				SolidColorBrush background = new SolidColorBrush(Color.FromArgb(byte.MaxValue, byte.MaxValue, byte.MaxValue, byte.MaxValue));
				foreach (object obj in this.leftWrapPanel.Children)
				{
					if (obj is LeftButton)
					{
						(obj as LeftButton).button.Background = background;
					}
				}
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message);
			}
		}

		private void btn_ClickItem(object sender, RoutedEventArgs e)
		{
			if (this.ClickItem != null)
			{
				this.SetLeftButtonBackGroundToWrite();
				SolidColorBrush background = new SolidColorBrush((Color)ColorConverter.ConvertFromString("#BBCCCCCB"));
				((Button)sender).Background = background;
				this.ClickItem((Button)sender, e);
			}
		}

		private CommandTableInfos m_CommandTableInfos;

		private MainPage m_MainPage;

		private ExternalCommandData m_CommandData;
	}
}
